package com.cloudkinto.service.order.delayTask;

import lombok.Data;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * 获取面单延迟任务
 *
 * @author long
 */
@Data
public class GetShippingLabelDelay implements Delayed {

    /**
     * 延迟截止时间（单位：毫秒）
     */
    private Long delayTime = System.currentTimeMillis();
    /**
     * 数据map
     */
    public final Map<String, Long> dataMap;
    /**
     * 任务时间戳
     */
    public String taskTimeStamp;

    /**
     * 任务添加时间
     */
    public LocalDateTime addTime;

    public GetShippingLabelDelay(Long delayTime, Map<String, Long> dataMap, String taskTimeStamp, LocalDateTime addTime) {
        this.delayTime += delayTime;
        this.dataMap = dataMap;
        this.taskTimeStamp = taskTimeStamp;
        this.addTime = addTime;
    }

    /**
     * 获取剩余时间
     *
     * @param unit the time unit
     * @return 剩余时间
     */
    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(delayTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    /**
     * 队列里元素的排序依据
     *
     * @param o the object to be compared.
     */
    @Override
    public int compareTo(Delayed o) {
        return Long.compare(this.getDelay(TimeUnit.MILLISECONDS), o.getDelay(TimeUnit.MILLISECONDS));
    }
}
